import { Http } from "../../utils/http"
import { Product } from "../../model/Product"

const app = getApp();

Page({

  data: {
    tabs: [
      //   {
      //   id: 0,
      //   name: "500元套餐： 500元 权益2选1",
      //   isActive: false
      // },
      // {
      //   id: 1,
      //   name: "1000元套餐： 1000元 权益4选2",
      //   isActive: false
      // },
      // {
      //   id: 2,
      //   name: "3000元套餐： 3000元 权益4选4(限购一次)",
      //   isActive: false
      // }
    ],
    spec: false,
    prodID:'',//选中商品的ID
    money:'',
    getImg:[],
    topImgx:'',
    a:[],
    product_right_ids:''
  },

  //生命周期函数--监听页面加载
  onLoad: async function (options) {
    let a = await Http.login();
    let result = await Product.get_product_detail_imgs();
    let productList = await Product.get_product_genesis_product_list();

    //页面初始化
    app.login((res) => {  
      wx.setStorageSync('register', res.register);
      wx.setStorageSync('shoplist', res.store_list);
      wx.setStorageSync('verifyman', res.verification);
      wx.setStorageSync('distributor', res.distributor);
      wx.setStorageSync('user_id', res.user_id);
      wx.setStorageSync('store_id', res.store_id);
      wx.setStorageSync('money', res.userinfo.money);
      wx.setStorageSync('payment_password', res.userinfo.payment_password);
      wx.setStorageSync('userinfo-mobile', res.userinfo.mobile)
      console.log('页面初始化成功');

      this.onProductlist(productList);
      this.onGetImg(result);
    });

    //对scene做处理
    if (options.scene == undefined || options.scene == null || options.scene == "" || options.scene == 0) {
      wx.removeStorageSync("scene");
    } else {
      let scene = decodeURIComponent(options.scene);  //解码场景值
      console.log(scene);
      wx.setStorageSync('scene', scene);  //设置场景值
    }

  },

  //获取图片
  onGetImg: async function (result) {
    if (result.code == 0) {
      this.setData({
        a:result.data.reverse()
      })
      this.setData({
        topImgx:this.data.a[0],
        topImgs:this.data.a[1],
        getImg:this.data.a
      })
    } else {
      console.log("获取图片失败 方法名：onGetImg");
    }
  },

  //获取商品规格
  onProductlist: async function (productList) {
    if (productList.code == 0) {
      let money,prodId,product_right_ids;
      for (let index = 0; index < productList.data.length; index++) {
        money =   productList.data[1].price;
        prodId = productList.data[1].id;
        product_right_ids=productList.data[1].product_right_ids;
        productList.data[1]['isActive'] = true;
      }
      this.setData({
        tabs:productList.data,
        money :money,
        product_right_ids
      })
    } else {
      console.log("获取商品规格失败 方法名：onProductlist");
    }
  },

  //红色标记选中套餐
  tab: function (e) {
    console.log(e.currentTarget.dataset);
    let tab = this.data.tabs;
    let money,product_right_ids;
    for (let index = 0; index < tab.length; index++) {
      if (tab[index].id == e.currentTarget.dataset.index) {
        tab[index].isActive = true;
        wx.setStorageSync('tabs',  tab[index]);
        money = tab[index].price;
        product_right_ids = tab[index].product_right_ids;
      } else {
        tab[index].isActive = false;
      }
    }
    this.setData({
      tabs: tab,
      prodID:e.currentTarget.dataset.index,
      money: money,
      product_right_ids
    })
  },

  //弹起套餐选择
  spec: function () {
    this.setData({
      spec: true
      
    })
  },
  
  //关闭套餐选择
  onClose: function () {
    this.setData({
      spec: false
    })
  },

  //点击确认购买套餐按钮
  goPay: function (){
    if(this.data.prodID == '' || this.data.prodID == undefined){  //没选择套餐发生的事件
      wx.showToast({
        title: "请选择套餐",
        duration: 2000,
        icon: 'none'
      });
    }else if (wx.getStorageSync('register') == 0 || wx.getStorageSync('register') == null) {  //没注册发生的事件
      wx.showModal({
        title: '提示',
        content: "您还未注册，是否先注册",
        success: function (res) {
          if (res.cancel) {
            //点击取消,默认隐藏弹框
          } else {
            //点击确定
            wx.navigateTo({
              url: '../logon/index'
            });
          }
        }
      });
    }else { //正确时发生的事件
    let tab = this.data.tabs;
    let money,product_right_ids;
    for (let index = 0; index < tab.length; index++) {
      if (tab[index].id == this.data.prodID) {
        tab[index].isActive = true;
        wx.setStorageSync('tabs',  tab[index]);
        money = tab[index].price;
        product_right_ids = tab[index].product_right_ids;
      } else {
        tab[index].isActive = false;
      }
    }
    
    
      wx.navigateTo({ //跳转到支付index
        url: '/pages/pay/index'
      })
      wx.setStorageSync('product_right_ids', product_right_ids)
      wx.setStorageSync('prodID', this.data.prodID)
      wx.setStorageSync('placeMoney', money)
    }
  },

  //转发
  onShareAppMessage: function (e) {
    console.log();
    return {
      title: '送自己与闺蜜花之盛宴',
      path: '/pages/home/index',
      imageUrl: 'https://img01.yzcdn.cn/upload_files/2021/05/03/Fi6DGCCc5i5Y2x3eeZxY2wQlWUEp.png'
    }
  },

  clickIndex(event) {
    console.log("-------");
    console.log(event.target);
    console.log(event.target.dataset.id);
  }

})